include("../../static/js/config.js");
include("../../static/js/common.js");

var form;
var data = [];

$(function () {

    layui.use(['element', 'tree', 'layer', 'form', 'upload', 'treeGrid'], function () {
        var $ = layui.jquery, tree = layui.tree;

        form = layui.form;
        
        asyncRequest('/admin/resource/list', {}, function (res) {
            if (res.code === 200){
                data = res.data;
            }
        });

        var treeGrid = layui.treeGrid;
        var treeTable = treeGrid.render({
            elem: '#table',
            data: data,
            cellMinWidth: 200,
            treeId: 'id',
            treeUpId: 'parentId',
            treeShowName: 'name',
            cols: [[
                {field: 'name', width:200, title: '菜单'},
                {field: 'page', width:200, title: '页面'},
                {
                    field: 'icon', width:200, title: '图标',
                    templet: function (d) {
                        if (d.icon){
                            return '<i class="iconfont">' + d.icon + '</i>'
                        }
                        return '';
                    }
                },
                {field: 'orders', width:200, title: '顺序'},
                {field: 'typeName', width:200, title: '类型'},
                {
                    title: '操作', align: 'left',
                    templet: function (d) {
                        var html = '';
                        if (d.parentId === -1){
                            html += '<a class="layui-btn layui-btn-primary layui-btn-xs" onclick="addResource('+ d.id +')" lay-event="add">添加</a>';
                            return html;
                        }
                        if (d.parentId === 0){
                            html += '<a class="layui-btn layui-btn-primary layui-btn-xs" onclick="addResource('+ d.id +')" lay-event="add">添加</a>';
                        }
                        html += '<a class="layui-btn layui-btn-warm layui-btn-xs" onclick="updateResource('+ d.id +')" lay-event="del">编辑</a>';
                        html += '<a class="layui-btn layui-btn-danger layui-btn-xs" onclick="delResource('+ d.id +')" lay-event="del">删除</a>';
                        html += '<a class="layui-btn layui-btn-normal layui-btn-xs" onclick="bindAuthority('+ d.id +')" lay-event="del">权限</a>';
                        return html;
                    }
                }
            ]],
            page: false,
            parseData: function (res) {
                reqAfter(res, function (data) {});
                return {
                    "code": res.code === 200 ? 0 : res.code,
                    "msg": res.msg,
                    "count": res.data.total,
                    "data": res.data.records
                };
            }
        });
    });

});


function saveRs() {
    request('/admin/resource/save',
        {parentId: $('#parentId').val(), name: $('#name').val(), page: $('#page').val(), icon: $('#icon').val(), orders: $('#orders').val(), type: $('#type option:selected').val()},
        function (data) {
        if (data.code === 200){
            layer.closeAll();
            layer.msg(data.msg, {icon: 1});
            setTimeout(function () {
                window.location.reload();
            }, 3000);
        } else {
            layer.msg(data.msg, {icon: 5});
        }
    })
}

function updateRs() {
    request('/admin/resource/update',
        {id: $('#id').val(), name: $('#name2').val(), page: $('#page2').val(), icon: $('#icon2').val(), orders: $('#orders2').val(), type: $('#type2 option:selected').val()},
        function (data) {
        if (data.code === 200){
            layer.closeAll();
            layer.msg(data.msg, {icon: 1});
            setTimeout(function () {
                window.location.reload();
            }, 3000);
        } else {
            layer.msg(data.msg, {icon: 5});
        }
    })
}


function addResource (id) {
    $('#parentId').val(id);
    layer.open({
        title: '添加',
        type: 1,
        skin: 'layui-layer-rim',
        area: ['420px', '420px'],
        content: $("#addContent"),
        success: function(layero, index){
            form.render('select');
        }
    });
}

function updateResource(id) {
    var elem = {};
    for (var i = 0; i < data.length; i++){
        var item = data[i];
        if (item.id === id) {
            elem = item;
        }
    }

    $('#id').val(id);
    $('#name2').val(elem.name);
    $('#icon2').val(elem.icon);
    $('#orders2').val(elem.orders);
    $('#type2').find('option[value="' + elem.type + '"]').attr('selected', true);

    layer.open({
        title: '编辑',
        type: 1,
        skin: 'layui-layer-rim',
        area: ['420px', '420px'],
        content: $("#updateContent"),
        success: function(layero, index){
            form.render('select');
        }
    });
}

function delResource(id) {
    request("/admin/resource/delete", {id:id}, function (data) {
        if (data.code === 200){
            layer.msg(data.msg, {icon: 1});
            setTimeout(function () {
                window.location.reload();
            }, 3000);
        } else {
            layer.msg(data.msg, {icon: 5});
        }
    });
}

function bindAuthority(id) {
    layer.open({
        area:['80%', '80%'],
        type: 2,
        content: config.url + '/page/authority/selectList.html?resourceId=' + id
    });
}




